<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/head.png" alt="" />
      </div>

      <!-- 登录表单 -->
      <el-form
        :model="loginFormData"
        label-width="60px"
        class="login_form"
        :rules="loginFormRules"
        ref="loginForm"
      >
        <el-form-item label="账号" prop="phone">
          <el-input v-model="loginFormData.phone" placeholder="请输入手机号">
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginFormData.password" placeholder="请输入密码">
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-row justify="end">
          <el-form-item class="login_btn">
            <el-button type="primary" @click="login" :loading="loading"
              >登录</el-button
            >
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import authApi from "@/api/authApi";
import userUtils from "@/utils/userUtils";

export default {
  name: "Login",
  data() {
    return {
      loading: false,
      // 表单数据
      loginFormData: {
        phone: "13100000000",
        password: "123456",
      },
      // 表单校验规则
      loginFormRules: {
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 登录请求
    login() {
      this.$refs["loginForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          authApi.login
            .loginWithPhone(this.loginFormData)
            .then((res) => {
              userUtils.saveUserInfo(res.data);
              this.$message.success({
                message: "登录成功",
                duration: 1500,
                onClose: () => {
                  this.$router.push("/home");
                },
              });
            })
            .catch((error) => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    // 重置表单数据
    reset() {
      this.$refs["loginForm"].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #e7eaee;
  height: 100vh;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.login_btn {
  display: flex;
  justify-content: flex-end;
}
</style>